package main

import (
	"fmt"
	"net/http"
	"os"
	"regexp"
	"strconv"
)

//https://movie.douban.com/top250?start=50&filter=

//https://movie.douban.com/top250?start=25&filter=

func HttpGet2(sendUrl string) (result string){

	resp, err := http.Get(sendUrl)
	if err != nil {
		fmt.Println("出错", err)
		return
	}

	defer resp.Body.Close()

	buf := make([]byte, 4096)
	//循环爬取整页数据
	for {
		n, err:= resp.Body.Read(buf)
		if n <= 0 {
			break
		}
		if err != nil {
			fmt.Println("出现错误", err)
			break
		}
		result += string(buf[:n])
	}
	return
}

func Save2file(filename string)  {
	path := "c:/img/"
	file, err := os.Create(path + filename + ".txt")

	if err != nil {
		fmt.Println("出现错误", err)
		return
	}

	defer file.Close()

	_, _ = file.WriteString(filename + "\t\t\t" + "这个是内容，你看看")

}


func SpiderPage(index int, page chan int)  {

	//获取url
	url := "https://movie.douban.com/top250?start="+strconv.Itoa((index-1)*25)+"&filter=";

	result := HttpGet2(url)

	// 解析，编译正则表达式
	ret := regexp.MustCompile(`<img width="100" alt="(?:(.*?))"`)
	//提取需要信息
	var fileName [][] string = ret.FindAllStringSubmatch(result, -1)

	for i := 0; i<len(fileName); i++ {
		Save2file(fileName[i][1])
	}

	page <- index

}

func DowloadPage()  {
	url := "http://www.umoer.com/web/ptmt.html?p=2"

	result := HttpGet2(url)

	var hostName string = "http://www.umoer.com/"

	ret := regexp.MustCompile(`<img src="(.*?)"  alt="`)

	imageChannel := make(chan int)

	filename := ret.FindAllStringSubmatch(result, -1)
	for i:=0; i<len(filename); i++ {
		imageUrl := filename[i][1]
		go saveImag(hostName+imageUrl, i, imageChannel)
	}

	for i:=0; i<len(filename); i++ {
		fmt.Println(<- imageChannel)
	}
}

func saveImag(sendUrl string, index int, imageChannel chan int)  {

	path := "C:/img/" + strconv.Itoa(index) + ".jpg"
	f, err := os.Create(path)
	if err != nil {
		return
	}
	defer f.Close()

	resp, err :=http.Get(sendUrl)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	buf := make([]byte, 4096)
	for {
		n, err := resp.Body.Read(buf)
		if err != nil {
			break
		}
		f.Write(buf[:n])
	}
	imageChannel <- index
}


func toWork(start, end int)  {
	fmt.Printf("正在爬取 %d 到 %d", start, end)

	DowloadPage();

	//page := make(chan int) //子Go程与主Go程完成同步，意思是子Go程没有全部执行完毕，主Go程不许退出。
	//
	//for i:=start; i<=end; i++ {
	//	go SpiderPage(i, page)
	//}
	//
	//for i:=start; i<=end; i++ {
	//	fmt.Println("爬取完成", <- page)
	//}
}

func main()  {

	//横向爬取
	var start, end int
	//fmt.Print("请输入开始爬取的页数 start")
	//fmt.Scan(&start)
	//fmt.Print("请输入爬取的终止页 end")
	//fmt.Scan(&end)

	toWork(start, end)

	//纵向爬去
}
